﻿Imports System.Data.SqlClient

Public Class frmDependent


    Sub FillDependants()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("select * from Dependent Where EmpNo = " & Me.txtEmpNo.Text, cnn)
            Dim Reader As SqlDataReader

            Me.GridDependents.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.GridDependents.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("DependentName"), Reader.Item("Gender"), _
                                                         Reader.Item("Relationship"), CDate(Reader.Item("DoB")).ToString("yyyy/MM/dd"), _
                                                         Reader.Item("PassHolder"), Reader.Item("PassportNo"), Reader.Item("IssuePlace"), _
                                                         CDate(Reader.Item("IssueDate")).ToString("yyyy/MM/dd"), _
                                                         CDate(Reader.Item("ExpDate")).ToString("yyyy/MM/dd")})
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Me.txtDependent.Text.Trim.Length = 0 Or Me.ComboRelationship.SelectedIndex = -1 Then
            MsgBox("Please complete dependent information")
        Else
            Try
                Me.Cursor = Cursors.WaitCursor
                Dim cmd As New SqlCommand("Insert Into Dependent (EmpNo, EmpName, Position, Branch, DependentName, Relationship, DoB," & _
                                          "PassHolder, PassportNo, ExpDate, IssuePlace,IssueDate, Gender)  Values (N'" & Me.txtEmpNo.Text & _
                                          "',N'" & Me.TxtName.Text & "',N'" & Me.txtPosition.Text & "',N'" & Me.txtBranch.Text & _
                                          "',N'" & Me.txtDependent.Text & "',N'" & Me.ComboRelationship.Text & _
                                          "',N'" & Me.DTBirth.Value.ToShortDateString & "'," & Me.chkPassport.CheckState & ",N'" & Me.txtPassportNo.Text & _
                                          "',N'" & Me.DTExpireDate.Value.ToShortDateString & "',N'" & Me.txtPssprtIsuPlce.Text & "',N'" & Me.DTPassprtIssue.Value & "',N'" & Me.CombGender.Text & "')", cnn)

                cnn.Open()
                cmd.ExecuteNonQuery()
                cnn.Close()

                FillDependants()
                Clear()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub frmDependent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillDependants()
    End Sub

    Sub Clear()
        Me.txtPassportNo.Text = ""
        Me.CombGender.SelectedIndex = -1
        Me.txtPassportNo.Clear()
        Me.txtPssprtIsuPlce.Clear()
        Me.chkPassport.CheckState = 0
        Me.ComboRelationship.SelectedIndex = -1
        Me.txtDependent.Text = ""
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        Clear()
    End Sub

    Private Sub GridDependents_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridDependents.CellClick
        If e.ColumnIndex = Me.DGColEdit.Index Then
            Dim a As New frmEditDependant
            a.DepID = Me.GridDependents.CurrentRow.Cells("Sno").Value
            a.Branch = Me.txtBranch.Text
            a.EmpName = Me.TxtName.Text
            a.Position = Me.txtPosition.Text
            a.Empno = Me.txtEmpNo.Text
            a.ShowDialog()

            FillDependants()

        ElseIf e.ColumnIndex = Me.DGColDelete.Index Then
            If MsgBox("Confirm deleting: " & Me.GridDependents.CurrentRow.Cells(1).Value, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    Me.Cursor = Cursors.WaitCursor

                    Dim cmd As New SqlCommand("Delete From Dependent Where SNo=" & CInt(Me.GridDependents.CurrentRow.Cells(0).Value), cnn)


                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()

                    FillDependants()

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub
End Class